package com.ebeitech.library.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by alice on 2017/11/8.
 */
public class TimeUtil {

	public static final String UNIT_DAY = "day";
	public static final String UNIT_HOUR = "hour";
	public static final String UNIT_MINUTE = "minute";
	public static final String UNIT_SECOND = "second";

	public static final SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);

	public static String getCurrentTime(){
		return sdfTime.format(new Date());
	}

	public static String getCurrentTime(String format){
		if(StringUtil.isStringNullOrEmpty(format)) return null;
		return new SimpleDateFormat(format, Locale.CANADA).format(new Date());
	}

	public static String getCurrentDay(){
		return getDayStrFromTime(getCurrentTime());
	}

	public static String getDayStrFromTime(String time){
		return time.substring(0, 10);
	}

	public static String getDateToString(Date date){
		return sdfTime.format(date);
	}

	public static String getDateToString(Date date, String format){
		return new SimpleDateFormat(format, Locale.CHINA).format(date);
	}

	public static String getFormatTimestamp(String time) {
		if (StringUtil.isStringNullOrEmpty(time)) {
			return "";
		}
		if (!time.contains(":")) {
			time += " 00";
		}
		while (time.split(":").length < 3) {
			time += ":00";
		}
		return time + ".00";
	}

	public static long getTimeMillis(String time) {
		return Timestamp.valueOf(getFormatTimestamp(time)).getTime();
	}

	/**
	 * HH:mm:ss 增加为23:59:59
	 * @param time yyyy-MM-dd格式
	 * @return
	 */
	public static String getQuestionEndTime(String time){
		if(StringUtil.isStringNullOrEmpty(time)){
			time = getCurrentTime("yyyy-MM-dd");
		}
		return time + " 23:59:59";
	}

	public static long stringTimeToLongTime(String time){
		if(StringUtil.isStringNullOrEmpty(time)){
			return 0;
		}
		try {
			return sdfTime.parse(time).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static long stringTimeToLongTime(String time, String format){
		try {
			return new SimpleDateFormat(format).parse(time).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static String getTimeWithDiff(String time, int diff, String unit) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(getTimeMillis(time));
		if (UNIT_HOUR.equals(unit)) {
			calendar.add(Calendar.HOUR, diff);
		} else if (UNIT_DAY.equals(unit)) {
			calendar.add(Calendar.DAY_OF_YEAR, diff);
		}else if (UNIT_MINUTE.equals(unit)) {
			calendar.add(Calendar.MINUTE, diff);
		}else if (UNIT_SECOND.equals(unit)) {
			calendar.add(Calendar.SECOND, diff);
		}
		return sdfTime.format(calendar.getTime());
	}
	/**
	 * 计算相差的时间数
	 *
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static int calculateDiffTime(String startTime, String endTime, String unit) {
		long startTimeLong = getTimeMillis(startTime);
		long endTimeLong = getTimeMillis(endTime);
		long nd = 1000 * 60 * 60;// 一小时的毫秒数
		int diff = (int) ((endTimeLong - startTimeLong) / nd);
		if (StringUtil.isStringNullOrEmpty(unit)) {
			return diff;
		}
		if (UNIT_HOUR.equals(unit)) {
			return diff;
		} else if (UNIT_MINUTE.equals(unit)) {
			nd = 1000 * 60;// 一分钟的毫秒数
			diff = (int) ((endTimeLong - startTimeLong) / nd);
			return diff;
		} else if (UNIT_SECOND.equals(unit)) {
			nd = 1000;// 一秒的毫秒数
			diff = (int) ((endTimeLong - startTimeLong) / nd);
			return diff;
		} else if (UNIT_DAY.equals(unit)) {
			nd = 1000 * 60 * 24;// 一秒的毫秒数
			diff = (int) ((endTimeLong - startTimeLong) / nd);
			return diff;
		}
		return diff;
	}

	/**
	 * 获取时间的展示
	 * 例子：时间展示
	 * 展示规则：如果是当天只显示时间 08:00 - 09:00,
	 * 		   如果不是今天但处在今年内就显示日期 01-01 08:00
	 * 		   如果有时间不在今年内就显示年份 2017-01-01 08:00
	 * @param time
	 * @return
	 */
	public static String getDisplayTime(String time) {
		if (StringUtil.isStringNullOrEmpty(time)) {
			return "";
		} else if (time.length() < 16) {
			return time;
		}
		String displayTimePeriods = "";
		String currentTime = getCurrentTime();
		String currentYear = currentTime.substring(0, 4);
		String currentDay = currentTime.substring(5, 10);
		if (!currentYear.equals(time.substring(0, 4))) {
			displayTimePeriods = time.substring(0, 16);
		} else if (!currentDay.equals(time.substring(5, 10))) {
			displayTimePeriods = time.substring(5, 16);
		} else {
			displayTimePeriods = time.substring(11, 16);
		}
		return displayTimePeriods;
	}
}
